<template>
  <el-dialog
      title="组件列表"
      v-el-drag-dialog
      v-if="showDom"
      :visible.sync="visible"
      width="80%"
      center
      @closed="closed">
    <el-table
        class="yt-table yt-table-thead"
        :data="pluginsLists"
        border
        style="width: 100%" size="mini">
      <el-table-column prop="" label="选择" width="60px" align="center">
        <template slot-scope="scope">
          <el-radio :label="scope.row.id" v-model="pluginId"></el-radio>
        </template>
      </el-table-column>
      <el-table-column
          prop="pluginsName"
          label="组件名称"
          width="200px"
          show-overflow-tooltip></el-table-column>
      <el-table-column prop="pluginsVesion" label="版本" width="150px"></el-table-column>
      <el-table-column
          prop="pluginsPath"
          label="组件路径"
          min-width="100px"
          show-overflow-tooltip></el-table-column>
      <el-table-column prop="pluginsStatus" label="组件状态" width="100px" align="center">
        <template slot-scope="scope">
          <el-tag v-if="parseInt(scope.row.pluginsStatus)===0" type="success" size="mini"
                  effect="dark">运行
          </el-tag>
          <el-tag v-else-if="parseInt(scope.row.pluginsStatus)===1" type="success" size="mini"
                  effect="dark">未运行
          </el-tag>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="pluginsTest" label="测试结果" width="100px" align="center">
        <template slot-scope="scope">
          <el-tag v-if="parseInt(scope.row.pluginsTest)===0" type="success" size="mini"
                  effect="dark">通过
          </el-tag>
          <el-tag v-else-if="parseInt(scope.row.pluginsTest)===1" type="success" size="mini"
                  effect="dark">未通过
          </el-tag>
          <el-tag v-else type="danger" size="mini" effect="dark">未测试</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="pluginsScene"
          label="场景描述"
          min-width="100px"
          show-overflow-tooltip></el-table-column>
      <el-table-column
          prop="remarks"
          label="备注"
          min-width="100px"
          show-overflow-tooltip></el-table-column>
    </el-table>

    <div style="margin-top: 15px;">
      <el-pagination
          class="yt-pagination"
          background
          layout="total, prev, pager, next"
          :current-page.sync="currentPage"
          :page-size.sync="pageSize"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange">
      </el-pagination>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button size="small" type="" @click="hide">关 闭</el-button>
      <el-button size="small" type="primary" @click="confirmTap">确 认</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "PluginsDailog",
  data () {
    return {
      showDom: false,
      visible: false,
      pluginsLists: [],
      // 组件列表
      total: 0,
      currentPage: 1,
      pageSize: 10,
      pluginId: ''
    };
  },
  computed: {},
  created () {
    this.getPluginsByUser()
  },
  mounted () {

  },
  methods: {
    show () {
      this.showDom = true
      this.visible = true
    },
    hide () {
      this.visible = false
    },
    closed () {
      this.showDom = false
    },
    getPluginsByUser () {
      this.$api.getPluginsByUserPage({
        pageNum: this.currentPage,
        pageSize: this.pageSize
      }).then(res => {
        this.pluginsLists = res.data.data.records
        this.total = res.data.data.total
      })
    },
    handleSizeChange () {
      this.getPluginsByUser()
    },
    handleCurrentChange () {
      this.getPluginsByUser()
    },

    confirmTap () {
      if (!this.pluginId) {
        return this.$message.error('请选择组件')
      }
      let selectItem = this.pluginsLists.find(item => {
        return item.id === this.pluginId
      })
      if (selectItem) {
        this.$emit('checked', selectItem)
      }
      this.hide()
    }
  }
}
</script>

<style scoped lang="less">
  .el-pagination {
    position: relative;
    white-space: nowrap;
    padding: 10px 10px;
    font-weight: 700;

    /deep/ .el-pagination__total {
      color: #ffffff;
    }

    /deep/ .el-pagination__jump {
      color: #ffffff;
    }
  }
</style>